package exo2;

//import exo1.LectureEcritureVehicules;
import exo1.Vehicule;
import java.io.*;
import java.util.*;

public class LectureEcritureVendeur {
	
	private static ArrayList<Vendeur> readVendeurs(Scanner in)
	{
		ArrayList<Vendeur> vendeurs = new ArrayList<Vendeur>();
		while(in.hasNextLine())
		{
			Vendeur v = new Vendeur();
			v.readData(in);
			vendeurs.add(v);
		}
		return vendeurs;
	}
	private static void writeVendeurs(ArrayList<Vendeur> vendeurs, PrintWriter out)
	{
		for(Vendeur temp : vendeurs)
		{			
			temp.writeData(out);
		}
	}
	
	public static void main(
			String[] args) {
		
		PrintWriter out = null;
		Scanner in = null;
		
		try {
			out = new PrintWriter("vendeurs.txt");
			
			//Creation des voitures
			
			Vehicule v1 = new Vehicule(1,"Ford","Fiesta",2004,10000,5345,12);
			Vehicule v2 = new Vehicule(2,"Renault","Clio",2001,150000,2300,5);
			Vehicule v3 = new Vehicule(3,"Porsche","Carrera",2000,250000,50000,2);
			Vehicule v4 = new Vehicule(4,"Peugeot","208 GT",2012,120000,30000,5);
			
			//Creation des vendeurs
			
			Vendeur vendeur1 = new Vendeur("Toto"); 
			Vendeur vendeur2 = new Vendeur("Tata"); 
			Vendeur vendeur3 = new Vendeur("Titi");
			Vendeur [] vds = {vendeur1, vendeur2, vendeur3};
			
			//Repartition des vehicules par vendeur
			
			int [] achatV1 = {3,0,19}; // vendeur 1, vendeur 2, vendeur 3
			int [] achatV2 = {10,5,0};
			int [] achatV3 = {1,0,6};
			int [] achatV4 = {8,0,2};
			
			for(int i = 0 ; i < 3 ; i ++)
			{
				if(v1.venteExemplaires(achatV1[i])){
					vds[i].ajouterVehicule(v1.getId(), achatV1[i]);
				}
				if(v2.venteExemplaires(achatV2[i])){
					vds[i].ajouterVehicule(v2.getId(), achatV2[i]);
				}
				if(v3.venteExemplaires(achatV3[i])){
					vds[i].ajouterVehicule(v3.getId(), achatV3[i]);
				}
				if(v4.venteExemplaires(achatV4[i])){
					vds[i].ajouterVehicule(v4.getId(), achatV4[i]);
				}
			}
			
			vendeur1.writeData(out);
			vendeur2.writeData(out);
			vendeur3.writeData(out);
			
			BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream("vendeurs.txt"));
			in = new Scanner(bufferIn);
			
			ArrayList<Vendeur> vendeurs;
			vendeurs = LectureEcritureVendeur.readVendeurs(in);
			
			for(Vendeur temp : vendeurs)
			{
				System.out.println(temp);
			}
			LectureEcritureVendeur.writeVendeurs(vendeurs, out);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("ko");
		}
		
		in.close();
		out.close();

	}
}
